<?php

namespace Index\Controller;

use Think\Controller;

class IndexController extends Controller
{
    private $base_url = 'http://103.74.192.239:8090/api/';
    private $header = <<<EOF
{
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36",
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    "Host": "103.74.192.239:8090",
    "Origin": "http://onmk4s.cn"
}
EOF;

    public function index()
    {
        $arr = M("search")->where("is_delete = 0")->order("times desc")->limit(1000)->select();
        $this->assign('search_list',$arr);
        if (IS_GET) {
            $this->display();
        }

        if (IS_POST) {
            $key = htmlspecialchars(trim(urldecode($_POST['key'])));

            if(empty($key) || $key == "请输入的影视名称，尽量不要带数字、第几季和标点符号"){
                $this->display();
                return;
            }

            $member = $this->sortWeb($key);
            $r1 = $this->getXiaoy($key);
            $r2 = $this->getXxj($key);
            $r3 = $this->getGirls($key);

            // 如果有搜索结果的话
            if($member || $r1 || $r2 || $r3){
                // 保存搜索关键词
                $this->saveKeyword($key);
            }

            $this->assign('member',$member);
            $this->assign('one',$r1);
            $this->assign('two',$r2);
            $this->assign('three',$r3);
            $this->display('list');
        }
    }

    private function saveKeyword($key){
        $mod = M("search");
        // 先查有没有搜索
        $r = M("search")->where("keyword='{$key}'")->count();
        if(empty($r)){
            // 新增
            $arr = ['keyword'=>$key,'create_time'=>date("Y-m-d H:i:s")];
            $mod->create($arr);
            $mod->add();
        }else{
            // 搜索次数加1
            M("search")->where("keyword='{$key}'")->setInc('times', 1);
            M("search")->where("keyword='{$key}'")->setField(['update_time'=>date("Y-m-d H:i:s")]);
        }
    }


    private function sortWeb($name)
    {
        $url = $this->base_url . 'sortWeb';

        $data = <<<EOF
{
    "name": "$name",
    "token": "i69",
    "tabN": "movie_200401lnj",
    "topNo": "10",
    "whr": "question like '%{$name}%' ",
    "orderBy": "isTop DESC, date_time",
    "orderType": "DESC",
    "keys": "question,answer"
    }
EOF;
        $r =  $this->send_post_curl_header($url, $this->header, $data);
        $arr = json_decode($r,true);
        return $arr['list'];
    }

    private function getXxj($name)
    {
        $url = $this->base_url . 'getXxj';
        $data = <<<EOF
{
        "name": "$name",
        "token": "i69"
    }
EOF;
        $r =  $this->send_post_curl_header($url, $this->header, $data);
        $arr = json_decode($r,true);
        return $arr['list'];
    }


    private function getXiaoy($name)
    {
        $url = $this->base_url . 'getXiaoy';
        $data = <<<EOF
    {
        "name": "$name",
        "token": "i69"
    }
EOF;
        $r =  $this->send_post_curl_header($url, $this->header, $data);
        $arr = json_decode($r,true);
        return $arr['list'];
    }

    private function getGirls($name)
    {
        $url = $this->base_url . 'getGirls';
        $data = <<<EOF
    {
        "name": "$name",
        "token": "i69"
    }
EOF;
        $r =  $this->send_post_curl_header($url, $this->header, $data);
        $arr = json_decode($r,true);
        return $arr['list'];
    }


    public function getDetail()
    {
        $recourse_path = $_POST['url'];
        $_name = $_POST['name'];
        $type = $_POST['type'];

        if($type=='one'){
            $url = $this->base_url . 'getSingleXiaoy';
        }elseif ($type=='two'){
            $url = $this->base_url . 'getSingleXxj';
        }elseif($type=='three'){
            $url = $this->base_url . 'getSingleGirls';
        }else{
            echo json_encode(['code'=>0,'msg'=>'系统出错，请稍后再试']);
            return;
        }


        $data = <<<EOF
{
        "url": "$recourse_path",
        "name": "$_name",
        "_name": "$_name",
        "token": "i69"
    }
EOF;
        $r = $this->send_post_curl_header($url, $this->header, $data);
        $arr = json_decode($r,true);
        $res = $arr['answer'];
        echo json_encode(['code'=>1,'data'=>$res]);
    }

    /**
     * 发送post
     * @param $url
     * @param $header_data
     * @param $post_data
     * @return bool|string 返回字符串
     */
    private function send_post_curl_header($url, $header_data, $post_data)
    {
        $header_data = json_decode($header_data, true);
        $post_data = json_decode($post_data, true);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header_data);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
        // 在成功连接服务器前等待多久
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
        // 连接成功后等待多久
        curl_setopt($ch, CURLOPT_TIMEOUT, 3);
        $sResult = curl_exec($ch);
        if ($sError = curl_error($ch)) {
            die('当前人数爆满，刷新再试');
        }
        curl_close($ch);
        return $sResult;
    }


}
